package com.seeyon.apps.seepkk.dao;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;

import com.seeyon.apps.kk.util.JdbcAgentUtil;
import com.seeyon.cap4.form.bean.FormTableBean;
import com.seeyon.ctp.common.dao.BaseDao;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.util.ParamUtil;
import com.seeyon.ctp.util.Strings;

public class SeepDaoImpl extends BaseDao implements SeepDao {

	@Override
	public String getFormCodeByFormId(Long formId) throws BusinessException {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder("select templete_number from ctp_template");
		sb.append(" where form_appid=:formId");
		Map params = new HashMap();
		params.put("formId", formId);
		List<Map> list = JdbcAgentUtil.doQuery(sb.toString(), params);
		if(CollectionUtils.isNotEmpty(list)){
			for(Map map: list){
				if(Strings.isNotEmpty(ParamUtil.getString(map, "templete_number", ""))){
					return ParamUtil.getString(map, "templete_number");
				}
			}
		}
		return null;
	}

	@Override
	public boolean validateBaseData(Long formId, Long toMasterId) throws Exception{
		StringBuilder sb = new StringBuilder("select id from cap_form_relation_record");
		sb.append(" where TO_FORM_ID=:formId and TO_MASTERDATA_ID=:toMasterId");
		Map params = new HashMap();
		params.put("formId", formId);
		params.put("toMasterId", toMasterId);
		List<Map> list = JdbcAgentUtil.doQuery(sb.toString(), params);
		if(CollectionUtils.isNotEmpty(list)){
			return false;
		}
		return true;
	}
}
